﻿/*
   Copyright 2011 Softisis

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FOM.Interfaces;

namespace FOM.Impl.Conditions
{
    public class IsNumberEqualsCond:ICondition
    {
        private readonly double _v;

        public IsNumberEqualsCond(double v)
        {
            _v = v;
        }

        /// <summary>
        /// Returns true if source object is an int, float, double, long, AND this value is equal
        /// to the value given to the constructor.
        /// </summary>
        /// <param name="ctx">The mapping context, with a bunch of info inside.</param>
        /// <returns>true / false</returns>
        public bool CanApplyMapping(MappingContext ctx)
        {
            if (ctx.ObjectSrc==null)
                return false;

            if (ctx.ObjectSrc.GetType() == typeof(double))
                return ((double)ctx.ObjectSrc) == _v;

            if (ctx.ObjectSrc.GetType() == typeof(float))
                return ((float)ctx.ObjectSrc) == _v;

            if (ctx.ObjectSrc.GetType() == typeof(long))
                return ((long)ctx.ObjectSrc) == _v;
            
            if (ctx.ObjectSrc.GetType() == typeof(int))
                return ((int)ctx.ObjectSrc) == _v;

            return false;

        }

        public virtual bool IgnoreChildMappings
        {
            get { return true; }
        }

        public ICondition Reverse()
        {
            return this;
        }
    }
}
